const os = require('os')
const spinners = require('cli-spinners');
const spinner = spinners.dots;
const { bgRed, bgBlue } = require('colorette');

/**
 * 判断是否在本地
 * @returns isLocal
 */
const isLocal = () => {
	const hostname = os.hostname();
	if (hostname === 'localhost' || hostname === '127.0.0.1') {
		return true
	} else {
		return false
	}
}

/**
 * 终端动画
 * @returns { start, stop, stopError } 
 */
const cliLoading = () => {
	class cliLodaing {
		constructor() {
			this.timer = null
		}
		start(text) {
			let i = 0
			this.timer = setInterval(() => {
				process.stdout.write(`\r${spinner.frames[i]} ${text}...`)
				i = (i + 1) % spinner.frames.length
			}, spinner.interval)
		}
		stop(text) {
			clearInterval(this.timer)
			this.timer = null
			console.log(`\n${bgBlue(text)}`);
		}
		stopError(text) {
			clearInterval(this.timer)
			this.timer = null
			console.log(`\n${bgRed(text)}`);
		}
	}
	const loading = new cliLodaing()

	return {
		stop: loading.stop,
		stopError: loading.stopError,
		start: loading.start
	}
}

module.exports = {
	isLocal,
	cliLoading,
}